"""创建Role类:
    回合制有效，两个人进入到一分房间，相互攻击
    初始血量 100  初始积分100
    名字，血量，积分，攻击,防御，当前所在房间，个人拥有装备，可以加攻击和防御
    攻击 a.attack(b),随机攻击(10-50)

    创建Game类：游戏交互式菜单：
        创建角色，创建房间，
        添加角色到房间，
        房间满员自动开始游戏

    创建Room类：房间
     房间名，房间编号，房间成员，房间最大人数
     加入房间：房间满员自动开始游戏(player1, player2 )
     room.add(player)
"""
import random

class Room:
    def __init__(self, name, max_players):
        self.name = name
        self.max_players = max_players
        self.members = []

    def add_member(self, character):
        self.members.append(character)


class Character:
    def __init__(self, name):
        self.name = name

class Role:
    def __init__(self, name,equipment):
        self.name = name
        self.health = 100 #初始化血量
        self.score = 100 # 初始化积分
        self.attack_points = 0  # 表示角色的攻击能力，默认初始值为 0。
        self.defense_points = 0 # 表示角色的防御能力，默认初始值为 0。
        self.current_room = None
        self.equipment = equipment

    # def add_attack(self, points):
    #     self.attack_points += points  # 增加攻击力
    #
    # def add_defense(self, points):
    #     self.defense_points += points # 增加防御力
        for i in self.equipment:
            if i > 0:
                self.attack_points += i # 大于零为增加攻击力
            else:
                self.defense_points -= i# 负数或零为防御力
        # print(self.defense_points)
    def attack(self, other):
        if self.current_room != other.current_room:
            print("对方不在同一房间，无法攻击！")
            return
        damage1 = random.randint(10, 50)
        # print(damage1)
        damage = damage1 + self.attack_points
        damage -= other.defense_points #实际伤害为攻击减对方防御
        if damage > 0:
            other.health -= damage
            print(f"{self.name} 对 {other.name} 发起了攻击，造成了 {damage} 点伤害")
        else:
            print(f"{self.name} 的攻击被 {other.name} 的防御抵消了")
        if other.health <= 0:
            print(f"{other.name} 被击败了！")
            self.score += 10
            other.score -= 10

class Game:
    def __init__(self):
        self.characters = []
        self.rooms = []

    def create_character(self, name):
        character = Character(name)
        self.characters.append(character)
        print(f"Character {name} created.")
        return character

    def create_room(self, name, max_players):
        room = Room(name, max_players)
        self.rooms.append(room)
        print(f"{name} created.")
        return room

    def add_character_to_room(self, character, room):
        if len(room.members) >= room.max_players:
            print("Room is full. Starting game...")
            self.start_game(room)
        else:
            room.add_member(character)
            print(f"Character {character.name} added to {room.name}.")

    def start_game(self, room):
        print(f"The game has started in {room.name} with players:")
        for member in room.members:
            print(member.name)

# 创建游戏对象
game = Game()

# 创建角色和房间
character1 = game.create_character("Player1")
character2 = game.create_character("Player2")
room = game.create_room("Room 1", 2)

# 添加角色到房间
game.add_character_to_room(character1, room)
game.add_character_to_room(character2, room)

# 创建两个角色
player1 = Role("玩家1",[40,-6])
player2 = Role("玩家2",[68,-7])

# 将两个角色放入同一个房间
player1.current_room = "房间1"
player2.current_room = "房间1"

# 角色1攻击角色2
while player1.health > 0 and player2.health > 0:
    if player1.health > 0:
        player1.attack(player2)
    if player2.health > 0:
        player2.attack(player1)
print(f"当前{player1.name}的积分为{player1.score}")
print(f"当前{player2.name}的积分为{player2.score}")